#include"compile_run.hpp"
#include"../comm/httplib.h"

using namespace ns_complie_run;
using namespace httplib;

void Usage(std::string proc)
{
    std::cerr<<"Usage: "<<"\n\t"<<proc<<" port"<<std::endl;
}
int main(int argc,char* argv[])
{
    if(argc != 2) //没有按要求给予端口号
    {
        Usage(argv[0]);
        return 1;
    }
    Server svr;

    // 测试代码
    // svr.Get("/hello",[](const Request &req, Response &resp){
    //      resp.set_content("hello httplib,你好 httplib!", "text/plain;charset=utf-8");
    //  });

    //表示当有compile_run请求时再进行处理
    svr.Post("/compile_run",[](const Request &req, Response &resp){
        std::string in_json;
        in_json=req.body;
        std::string out_json;
        if(!in_json.empty())
        {
            Compile_Run::Start(in_json,&out_json);
            resp.set_content(out_json,"application/json;charset=utf-8"); //设置返回内容和格式
        }
    });  
    svr.listen("0.0.0.0",atoi(argv[1]));  //绑定监听
}